import { SUCCESS_CODE } from '@/utils/types.d';
import type { ResponseData } from '@/utils/types.d';
import request from '@/utils/request';
import type { PatrolRecordTableItem } from './types.d';
import type { PatrolDeviceTableItem } from '../Devices/Patrol/types';

export async function queryPatrolRecords(
  deviceSn: string,
  data: any,
  sort: any,
): Promise<{
  success: boolean;
  total: number;
  data: PatrolRecordTableItem[];
}> {
  const res: ResponseData<{ total: number; data: PatrolRecordTableItem[] }> = await request(
    `/api/patrol-record/v1.0.0/records/device/${deviceSn}`,
    {
      params: {
        data,
        sort,
      },
    },
  );
  if (res?.code === SUCCESS_CODE)
    return { success: true, total: res.data.total, data: res.data.data };
  return { success: false, total: 0, data: [] };
}

export async function getDeviceTrack(deviceSn: string, startTime?: string, endTime?: string) {
  const res: ResponseData<number[]> = await request(`/api/patrol-heart/v1.0.0/track/${deviceSn}`, {
    params: { startTime, endTime },
  });
  if (res?.message === 'success') {
    return { success: true, data: res.data };
  }
  return { success: false, errorMessage: res?.message || '获取失败' };
}
// /api/patrol-heart/v1.0.0/coordinate/{deviceSn}

export async function getDeviceCoordinate(deviceSn: string) {
  const res: ResponseData<[number, number]> = await request(
    `/api/patrol-heart/v1.0.0/coordinate/${deviceSn}`,
    {
      params: { deviceSn },
    },
  );

  return {
    success: true,
    data: res?.data || [0, 0],
  };
}

export async function getDeviceDetail(deviceSn: string) {
  const res: ResponseData<PatrolDeviceTableItem> = await request(
    `/api/patrol-device/v1.0.0/patrol-device/${deviceSn}`,
  );
  if (res?.code === SUCCESS_CODE) return { success: true, data: res.data, message: '获取成功' };
  return { success: false, data: null, message: res?.message || '获取失败' };
}
